package cn.chenjifeng.action;



import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.chenjifeng.imple.OrderImple;
import cn.chenjifeng.model.Desk_Id_Mdeol;
import cn.chenjifeng.model.OrderModel;
import cn.chenjifeng.model.Vip_id_model;
import cn.common.tool.seach;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class OrderAction extends ActionSupport
{
	HttpServletResponse response =  ServletActionContext.getResponse();
	HttpServletRequest request = ServletActionContext.getRequest();
	ArrayList<Desk_Id_Mdeol> list_d_id;
	String result_json;
	
	public String SearchAll() throws IOException, SQLException
	{
		
		
		ArrayList<OrderModel> list = null ;
		String json=null;
		seach b = new seach();
		OrderImple a = new OrderImple();
		ResultSet rs;
		int total;
		String json_s;
		String sql=null;
		
		String keyword = request.getParameter("keyword");
		String type= request.getParameter("type");
		String page_s = request.getParameter("pageNumber");
		String pagesum_s = request.getParameter("pageSize");
			
		if(page_s!=null && pagesum_s!=null)
		{	
			int page 	= Integer.parseInt(page_s);
			int pagesum = Integer.parseInt(pagesum_s);
			page = (page-1)*pagesum;
			
			if(keyword!=null)//判断是否查询部分
			{
				list = a.SearchAll_part(page, pagesum, type, keyword);
			}
			else//查询全部
			{
				list = a.SearchAll(page, pagesum);
			}
		}
		else
		{
			System.out.println("页数异常，page和pagesize ："+page_s+" "+pagesum_s);
		}
		
		

		try 
		{
		
			total = a.total();
			json_s = JSONArray.fromObject(list).toString();
			json_s="{\"total\":"+total  +
					",\"rows\":"+json_s +
					"}";
			System.out.println(json_s);
			
			
			/*-----封装餐桌id用于新增-----*/
			//list_d_id = a.SearchDeakId();
			//request.getSession().setAttribute("list", list_d_id);
			/*-----封装餐桌id用于新增-----*/
			
			
			
			response.reset();
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html; charset=utf-8");
			
			PrintWriter pw = response.getWriter();
			pw.write(json_s);
			
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return null;
	}

	
	
	
	public String AddOrder() throws IOException, SQLException
	{
		OrderModel a = new OrderModel();
		OrderImple b = new OrderImple();
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String time_data = df.format(new Date())+"";// new Date()为获取当前系统时间
		
		String aa[] = request.getParameter("property").split(",");
		a.setT_id(aa[0]);
		a.setV_id(aa[1]);
		a.setP_ac_time(aa[2]);
		a.setP_status("1");
		a.setP_time(time_data);
		
		int result = b.AddOrder(a);
		
		
		if(result==1)
		{
			result_json="1";
		}
		else if(result==0)
		{
			result_json="0";
		}
		

		response.reset();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		pw.write(result_json);
		
		return null;
		
	}
	
	public String Alert() throws SQLException//释放原有的餐桌号  把新的餐桌号绑定
, IOException
	{
		String property = request.getParameter("property");
		String p[] = property.split(",");	
		OrderModel a = new OrderModel();
		OrderImple b = new OrderImple();
		
		a.setP_id(Integer.parseInt(p[0]));
		a.setV_id(p[1]);
		a.setT_id(p[2]);
		a.setP_status(p[3]);
		a.setP_time(p[4]);
		a.setP_ac_time(p[5]);
		
		int result = b.Alert(a);
		
		if(result==1)
		{
			result_json="1";
		}
		else if(result==0)
		{
			result_json="0";
		}
		

		response.reset();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		pw.write(result_json);
		
		return null;
	}
	
	/*-------------查询餐桌id--------------------*/
	public String ShowD_id() throws SQLException, IOException
	{
		ArrayList<Desk_Id_Mdeol> list_d_id;
		OrderImple a = new OrderImple();
		
		list_d_id = a.SearchDeakId();
		
		String json_D_id = JSONArray.fromObject(list_d_id).toString();
		response.reset();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		pw.write(json_D_id);
		
		return null;
	}
	/*-------------查询餐桌id--------------------*/
	
	
	

/*---------------查询客户id---------------------*/	
	public String ShowV_id() throws SQLException, IOException
	{
		ArrayList<Vip_id_model> list_v_id;
		OrderImple a = new OrderImple();
		
		list_v_id = a.SearchVipId();
		//System.out.println("查询客户id用于增加预定订单时的list:"+list_v_id.get(0));
		String json_D_id = JSONArray.fromObject(list_v_id).toString();
		response.reset();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		pw.write(json_D_id);
		
		return null;
	}
	/*----------------查询客户id--------------------*/
	
	
	public String Alert_status() throws SQLException, IOException
	{
		String property = request.getParameter("proper");
		String p[] = property.split(",");	
		OrderModel a = new OrderModel();
		OrderImple b = new OrderImple();
		
		a.setP_id(Integer.parseInt(p[0]));
		a.setV_id(p[1]);
		a.setT_id(p[2]);
		a.setP_status(p[3]);
		a.setP_time(p[4]);
		a.setP_ac_time(p[5]);
		
		int result = b.Alert_status(a);
		
		if(result==1)
		{
			result_json="1";
		}
		else if(result==0)
		{
			result_json="0";
		}
		

		response.reset();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		pw.write(result_json);
		
		return null;
		
	}
	
	
	
	public ArrayList getList_d_id() {
		return list_d_id;
	}
}
